package com.tuanzi.loan.business.repository.statistics.impl;

import static com.tuanzi.loan.generated.jooq.stat.tables.PrdidChannelActiveUser.PRDID_CHANNEL_ACTIVE_USER;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.jooq.Condition;
import org.jooq.GroupField;
import org.jooq.SelectField;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.statistics.DayActivityProdChannelStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.ActivityAverageJoinStatRequest;
import com.tuanzi.loan.vo.response.statistics.ActivityAverageJoinStatResponse;

@Repository
public class DayActivityProdChannelStatRepositoryImpl extends JOOQBaseRepositoryPlus implements DayActivityProdChannelStatRepository{

	private static final String ALL_FIELD_ID = "'-1000'";
	private static final String ALL_FIELD = "'全部'";

	@Override
	protected TableImpl getTable() {
		return PRDID_CHANNEL_ACTIVE_USER;
	}

	public Page<ActivityAverageJoinStatResponse> getList(ActivityAverageJoinStatRequest searchRequest) {
		Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();

        selectFields.add(PRDID_CHANNEL_ACTIVE_USER.CDATE);
        groupFields.add(PRDID_CHANNEL_ACTIVE_USER.CDATE);

        if (Objects.nonNull(searchRequest.getStartDate())) {
            condition = condition.and(PRDID_CHANNEL_ACTIVE_USER.CDATE.ge(new java.sql.Date(searchRequest.getStartDate().getTime())));
        }
        if (Objects.nonNull(searchRequest.getEndDate())) {
        	condition = condition.and(PRDID_CHANNEL_ACTIVE_USER.CDATE.le(new java.sql.Date(searchRequest.getEndDate().getTime())));
        }

        // 产品
        if (Objects.isNull(searchRequest.getProductId()) || searchRequest.getProductId().isEmpty()) {
        	// 全部展开
        	selectFields.add(PRDID_CHANNEL_ACTIVE_USER.APP_PRD_ID);
            groupFields.add(PRDID_CHANNEL_ACTIVE_USER.APP_PRD_ID);
        } else if(isSelectQueryAll(searchRequest.getProductId())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("prdid"));
        } else {
            selectFields.add(PRDID_CHANNEL_ACTIVE_USER.APP_PRD_ID);
            groupFields.add(PRDID_CHANNEL_ACTIVE_USER.APP_PRD_ID);
            condition = condition.and(PRDID_CHANNEL_ACTIVE_USER.APP_PRD_ID.in(searchRequest.getProductId()));
        }

        // 渠道
        if (Objects.isNull(searchRequest.getChannelId()) || searchRequest.getChannelId().isEmpty()) {
        	// 全部展开
        	selectFields.add(PRDID_CHANNEL_ACTIVE_USER.CHANNEL);
            groupFields.add(PRDID_CHANNEL_ACTIVE_USER.CHANNEL);
        } else if(isSelectQueryAll(searchRequest.getChannelId())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("channel"));
        } else {
            selectFields.add(PRDID_CHANNEL_ACTIVE_USER.CHANNEL);
            groupFields.add(PRDID_CHANNEL_ACTIVE_USER.CHANNEL);
            condition = condition.and(PRDID_CHANNEL_ACTIVE_USER.CHANNEL.in(searchRequest.getChannelId()));
        }
        
        // 用户类型
        if (Objects.isNull(searchRequest.getUserType()) || searchRequest.getUserType().isEmpty()) {
        	// 全部展开
        	selectFields.add(PRDID_CHANNEL_ACTIVE_USER.IS_NEW_USER);
            groupFields.add(PRDID_CHANNEL_ACTIVE_USER.IS_NEW_USER);
        } else if(isSelectQueryAll(searchRequest.getUserType())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("is_new_user"));
        } else {
            selectFields.add(PRDID_CHANNEL_ACTIVE_USER.IS_NEW_USER);
            groupFields.add(PRDID_CHANNEL_ACTIVE_USER.IS_NEW_USER);
            condition = condition.and(PRDID_CHANNEL_ACTIVE_USER.IS_NEW_USER.in(searchRequest.getUserType()));
        }

        SelectQuery selectQuery = dslContext.select(selectFields)
                .select(DSL.sum(PRDID_CHANNEL_ACTIVE_USER.UV).as("uv"))
                .from(PRDID_CHANNEL_ACTIVE_USER)
                .where(condition)
                .groupBy(groupFields)
                .orderBy(PRDID_CHANNEL_ACTIVE_USER.CDATE.desc())
                .getQuery();

        List<ActivityAverageJoinStatResponse> resultList = selectQuery.fetch(record -> {
            ActivityAverageJoinStatResponse response = new ActivityAverageJoinStatResponse();
            response.setDate(record.get(0, Date.class));
            response.setProductId(record.get(1, String.class));
            response.setChannelId(record.get(2, String.class));
            response.setUserType(record.get(3, String.class));
            response.setActivity_uv(record.get(4, Integer.class));
            return response;
        });

		return new PageImpl<ActivityAverageJoinStatResponse>(resultList);
	}


	/******检查是否查询全部*********/
    private boolean isSelectQueryAll(List selectList) {
    	if(selectList == null || selectList.size() == 0) {
    		return false;
    	}
    	for(int i=0; i<selectList.size(); i++) {
    		String s = String.valueOf(selectList.get(i));
    		if("-1000".equals(s)) {
    			return true;
    		}
    	}
    	return false;
    }

}
























